package gui;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import backend.ProgramLauncher;
import backend.PropertiesInterperter;
/**
 * This is the main frame of the program.
 * @author Itai Raccah
 *
 */
public class ShortcutFrame extends JFrame implements 
		MouseListener,AWTEventListener,KeyListener  {
	public static boolean closeOnFocusLoss = true; 
	private Vector<Shortcut> shortcuts;
	private PropertiesInterperter properties;
	private static ShortcutFrame instance;
	public static ShortcutFrame getInstance() {
		return instance;
	}
	public static ShortcutFrame getInstance(Vector<Shortcut> shortcuts) {
		if (instance==null){
			instance = new ShortcutFrame(shortcuts);
		}
		return instance;
	}
	private ShortcutFrame(Vector<Shortcut> shortcuts) {
		super();
		this.shortcuts = shortcuts;
		this.properties = new PropertiesInterperter();
		this.setupThis();
		
	}

	public void setupThis() {
		
		final Toolkit tk = Toolkit.getDefaultToolkit();
	    tk.addAWTEventListener(this, AWTEvent.FOCUS_EVENT_MASK);
		this.add(getOuterPanel());
		this.setSize(properties.getWindowSize());
		this.setLocation(properties.getWindowLocation());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addMouseListener(this);
		this.setUndecorated(true);
	}

	private JPanel outerPanel;
	private JPanel getOuterPanel() {
		outerPanel = new JPanel(new FlowLayout());
		outerPanel.setBackground(properties.getBGColor());

		if (shortcuts.size()>0)
		for (Shortcut shortcut : shortcuts) {
			shortcut.getComponent().addKeyListener(this);
			outerPanel.add(shortcut.getComponent());
		}
		else
			outerPanel.add(new JLabel("You have no shortcuts added, run Config or ShortcutUtility.jar -config from the command line"));
		return outerPanel;
	}


	public void mouseClicked(MouseEvent e) {
		//ignore
	}

	public void mouseEntered(MouseEvent e) {
		//ignore
	}

	public void mouseExited(MouseEvent e) {
		Point point = e.getPoint();
		Dimension dim = this.getSize();
		if ((point.y <= dim.height && point.x <= dim.width))
			if (point.y >= 0 && point.x >= 0)
				return;

		System.exit(0);

	}

	public void mousePressed(MouseEvent e) {
		//ignore
	}

	public void mouseReleased(MouseEvent e) {
		//ignore
	}

	public void eventDispatched(AWTEvent event) {
		if (event instanceof FocusEvent){
			FocusEvent focusEvent = (FocusEvent) event;
			if (focusEvent.getID()==FocusEvent.FOCUS_LOST)
				if (focusEvent.getOppositeComponent()==null){
					if (closeOnFocusLoss)
						System.exit(0);
				}
		}
		}

	public void keyPressed(KeyEvent e) {

		//ignore

	}

	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode()==27)
			System.exit(0);
	}

	public void keyTyped(KeyEvent e) {

		//ignore

	}


}
